using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Data;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow;
using Deepcode.Flux.Core.Services.State;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_Config_Workflow_ui_Definitions : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxWorkflowEdit"; } }
	#endregion
	#region ViewState storage
	/// <summary>
	/// Stores the ID of the workflow handler being managed
	/// </summary>
	public string HandlerID
	{
		get
		{
			object o = ViewState["HandlerID"];
			if (o == null) return "";
			return (string)o;
		}
		set
		{
			ViewState["HandlerID"] = value;
		}
	}
	#endregion
	#region Page Load event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Get URL Parameters
			HandlerID = Request["HandlerID"];

			// Load the handler
			Handlers handlers = new Handlers();
			Handler h = handlers.GetHandlerByID(HandlerID);
			if (h == null) GoBack();
			
			// Setup display infos
			labHandler.Text = h.Name;

			// Bind the list of definitions to the grid
			BindDataGrid();
		}
	}
	#endregion
	#region Initialisation
	/// <summary>
	/// OnInit - hooks up the data paging handler, 
	/// rpp change handler and item command handlers
	/// </summary>
	/// <param name="e"></param>
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		datagrid.ItemCommand += new DataGridCommandEventHandler(GridCommand);
		datagrid.ItemDataBound += new DataGridItemEventHandler(datagrid_ItemDataBound);
	}
	#endregion
	#region Data binding and paging events
	/// <summary>
	/// Data binding
	/// </summary>
	private void BindDataGrid()
	{
		// Add javascript call to delete buttons
		datagrid.BindJavascript("btnDelete", "onclick", "return ConfirmDelete();");
	
		// Get and bind the data
		Definitions defs = new Definitions();
		datagrid.DataSource = defs.GetDefinitionsByHandlerID(HandlerID);
		datagrid.DataBind();
	}
	/// <summary>
	/// Item is being bound to a row in the grid
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator) return;

		LinkButton btnEditProcess	= (LinkButton) e.Item.FindControl("btnEditProcess");
		LinkButton btnDelete		= (LinkButton) e.Item.FindControl("btnDelete");
		Definition		d		= (Definition)e.Item.DataItem;
		WorkflowEngine	engine	= new WorkflowEngine(HandlerID);

		if( d == null ) return;
		if( ! engine.CanBeEdited( d.PK_ID ) ) btnEditProcess.Enabled = false;
		if (!engine.CanBeDeleted(d.PK_ID))
		{
			btnDelete.Enabled = false;
			btnDelete.Attributes.Remove("onclick");
		}
	}
	#endregion
	#region User interactions
	/// <summary>
	/// Command received from the data grid
	/// </summary>
	/// <param name="source"></param>
	/// <param name="e"></param>
	void GridCommand(object source, DataGridCommandEventArgs e)
	{
		if( e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator ) return;

		// Get the key of the row being actioned 
		int rowKey = (int) datagrid.DataKeys[e.Item.ItemIndex];

		// PROCESS COMMANDS
		if( e.CommandName == "ItemDelete" )
		{
			WorkflowEngine engine = new WorkflowEngine(HandlerID);
			if( ! engine.CanBeDeleted( rowKey )) return;

			Definitions defs = new Definitions();
			Definition def = defs.GetByID( rowKey );
			defs.DeleteByID( rowKey );

			QuickAudit.RaiseImportant(
				new AuditUser(),
				"Flux", "Workflow Manager",
				"deleted workflow definition ({0})",
				def.DefName );
			
			this.BindDataGrid();
			return;
		}
		if( e.CommandName == "ItemEdit" )
		{
			GoEditDefinition( rowKey );
			return;
		}
		if (e.CommandName == "ItemEditProcess")
		{
			WorkflowEngine engine = new WorkflowEngine(HandlerID);
			if( ! engine.CanBeEdited( rowKey )) return;
			GoListProcesses( rowKey );
			return;
		}
	}
	/// <summary>
	/// User clicked the add new button
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnAddNew_Click(object sender, EventArgs e)
	{
		GoAddDefinition();
	}
	/// <summary>
	/// User clicked the back button
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnBack_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Return the user BACK to the handlers list
	/// </summary>
	protected void GoBack()
	{
		string url = "ui_Handlers.aspx";
		Response.Redirect(url, true);
	}
	/// <summary>
	/// Send user forward to listing the processes for a definition
	/// </summary>
	/// <param name="DefinitionID"></param>
	protected void GoListProcesses( int DefinitionID )
	{
		string url = String.Format(
			"ui_Processes.aspx?HandlerID={0}&DefinitionID={1}",
			this.HandlerID,
			DefinitionID );
		
		Response.Redirect(url, true);
	}
	/// <summary>
	/// Send user forward to editing the definition
	/// </summary>
	/// <param name="DefinitionID"></param>
	protected void GoEditDefinition(int DefinitionID)
	{
		string url = String.Format(
			"ui_DefinitionsForm.aspx?HandlerID={0}&DefinitionID={1}", 
			this.HandlerID,
			DefinitionID);

		Response.Redirect(url, true);
	}
	/// <summary>
	/// Send user forward to creating a definition
	/// </summary>
	protected void GoAddDefinition()
	{
		string url = String.Format(
			"ui_DefinitionsForm.aspx?HandlerID={0}",
			this.HandlerID);

		Response.Redirect(url, true);
	}
	#endregion
}
